import { sys } from "cc";
import { hiyooo } from "../../core/HiYOOO";
import { StringUtil } from "../../core/utils/StringUtil";

export class GameQueryConfig {

    private  _data:any = null;

    
    constructor(){
        if (!sys.isBrowser) { // 非浏览器环境
            this._data = {}
            return;
        }
        this._data = this.parseUrl();
        if (!this._data['username']) {
            this._data['username'] = StringUtil.guid()
        }

    }

    private parseUrl() {
      if (typeof window != "object") return {};
      if (!window.document) {
        return {}
      }
      const url  = window.location.href.toString(); //获取当前页面的url
      let  u  = url.split("?"); //将url分割成参数和路径
      if (typeof u[1] == "string") {
        
        u = u[1].split("&"); //将参数分割成数组
        let  data:any  = {}; //定义一个对象，用于存放参数和值
        for (let i = 0; i < u.length; i++) {   

            let j = u[i]; //将参数和值分割成两个数组
            let x = j.indexOf("="); //判断是否有值
            if (x < 0) {
              continue;
            }
            let key = j.substring(0, x); //获取参数名
            let value = j.substring(x + 1); //获取参数值
            data[decodeURIComponent(key)] = value && decodeURIComponent(value); //将参数名和值存入对象中
        }

        return data;

      }else{
        return{}
      }

      
    }

}